package com.ruoyi.project.jxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.jxy.domain.Intention;
import com.ruoyi.project.jxy.service.IIntentionService;
import io.swagger.models.auth.In;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jxy.domain.Corpus;
import com.ruoyi.project.jxy.service.ICorpusService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 语料,保存有每个技能对应的语料.Controller
 *
 * @author ruoyi
 * @date 2019-12-06
 */
@RestController
@RequestMapping("/jxy/corpus")
public class CorpusController extends BaseController {
    @Autowired
    private ICorpusService corpusService;
    @Autowired
    private IIntentionService intentionService;

    /**
     * 查询语料,保存有每个技能对应的语料.列表
     */
    @PreAuthorize("@ss.hasPermi('jxy:corpus:list')")
    @GetMapping("/list")
    public TableDataInfo list(Corpus corpus) {
        startPage();
        QueryWrapper<Corpus> queryWrapper = new QueryWrapper<Corpus>();
        if(StringUtils.isNotBlank(corpus.getContent())){
            queryWrapper.like("content",corpus.getContent());
        }
        if(corpus.getIntentionId()!=null){
            queryWrapper.eq("intention_id",corpus.getIntentionId());
        }
        queryWrapper.orderByDesc("id");
        List<Corpus> list = corpusService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出语料,保存有每个技能对应的语料.列表
     */
    @PreAuthorize("@ss.hasPermi('jxy:corpus:export')")
    @Log(title = "语料,保存有每个技能对应的语料.", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Corpus corpus) {
        QueryWrapper<Corpus> queryWrapper = new QueryWrapper<Corpus>(corpus);
        List<Corpus> list = corpusService.list(queryWrapper);
        ExcelUtil<Corpus> util = new ExcelUtil<Corpus>(Corpus.class);
        return util.exportExcel(list, "corpus");
    }

    /**
     * 新增语料,保存有每个技能对应的语料.
     */
    @PreAuthorize("@ss.hasPermi('jxy:corpus:add')")
    @Log(title = "语料,保存有每个技能对应的语料.", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Corpus corpus) {
        if (corpus.getIntentionId()==null){
            return AjaxResult.error("意图ID为空");
        }
        if (StringUtils.isBlank(corpus.getContent())){
            return AjaxResult.error("语料内容为空");
        }
        if (StringUtils.isBlank(corpus.getType())){
            return AjaxResult.error("语料类型为空");
        }
        Map map = new HashMap<>();
        map.put("id", corpus.getIntentionId());
        map.put("content", corpus.getContent());
        //校验该技能下是否存在同样的语料
        List<Intention> relustList = intentionService.checkCorpusByIntentionId(map);
        if (relustList != null && relustList.size() > 0) {
            Intention checkIntetion = relustList.get(0);
            String errmsg = "语料不能有重复,%s中已存在该语料";
            return AjaxResult.error(String.format(errmsg, checkIntetion.getName()));
        }
        return toAjax(corpusService.saveCourpusInfo(corpus) ? 1 : 0);
    }


    /**
     * 删除语料,保存有每个技能对应的语料.
     */
    @PreAuthorize("@ss.hasPermi('jxy:corpus:remove')")
    @Log(title = "语料,保存有每个技能对应的语料.", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(corpusService.removeCorpusInfo(ids) ? 1 : 0);
    }
}
